package cn.how2j.chain;

import com.sun.prism.PresentableState;

import java.util.List;

public class FilterChain {
    private Filter[] filters;
    private Servlet servlet;
    int pos;

    public FilterChain(List<Filter> filters, Servlet servlet) {
        this.filters = filters.toArray(new Filter[]{});
        this.servlet = servlet;
    }

    public void doFilter(Request request, Response response) {
        if (pos < filters.length) {
            Filter filter = filters[pos++];
            filter.doFilter(request, response, this);
        } else {
            servlet.service(request, response);
        }
    }
}
